package com.appsindy.reserv.action;

import java.util.ArrayList;
import java.util.List;

import com.appsindy.common.AppsindyConstants;
import com.appsindy.common.action.AppsindyBaseAction;
import com.appsindy.common.bo.GlobalUser;
import com.appsindy.common.util.DateUtils;
import com.appsindy.reserv.ReservConstants;
import com.appsindy.reserv.vo.ReserveClearRoomTempVO;
import com.appsindy.reserv.bo.ReserveClearRoomTemp;
import com.appsindy.reserv.dao.ReserveClearRoomTempDAO;

public class ReservBackAction extends AppsindyBaseAction {
	
	private int roomId;
	private String roomCode;
	private ReserveClearRoomTempDAO reserveClearRoomTempDAO;
	
	public String clearRoom() {
        return "clearRoom";
    }
	
	public List<ReserveClearRoomTempVO> getTableData(){
		List<ReserveClearRoomTempVO> roomTempList = new ArrayList<ReserveClearRoomTempVO>();
		
		if(getRoomCode() == null || getRoomCode().trim().length() < 1){
			log.info("Init room temp");
			ReserveClearRoomTempVO roomTemp = new ReserveClearRoomTempVO();
			roomTemp.setUserLoginId(3);
			roomTemp.setOrgId(1);
			roomTemp.setServiceStatus(ReservConstants.ServiceStatusPayment);
			//roomTemp.setServiceStatus(ReservConstants.ServiceStatusBooked);
			try{
				getReserveClearRoomTempDAO().executeClearRoomTemp(roomTemp);
			} catch(Exception e){
				e.printStackTrace();
			}
			
			List<ReserveClearRoomTemp> roomTempL = getReserveClearRoomTempDAO().selectAllRoomTemp();
			if(roomTempL != null){
				for(int i=0; i<roomTempL.size(); i++){
					ReserveClearRoomTemp roomTempBO = roomTempL.get(i);
					
					ReserveClearRoomTempVO roomTempVO = new ReserveClearRoomTempVO();
					roomTempVO.setSeq(i+1);
					roomTempVO.setRoomId(roomTempBO.getRoomId());
					roomTempVO.setRoomCode(roomTempBO.getRoomCode());
					roomTempVO.setDateFrom(DateUtils.getDateStr(roomTempBO.getDateFrom(), false));
					roomTempVO.setDateTo(DateUtils.getDateStr(roomTempBO.getDateTo(), false));
					
					roomTempList.add(roomTempVO);
				}
			}
		} else{
			log.info("Search by room code : " + getRoomCode());
			ReserveClearRoomTemp roomTempBO = getReserveClearRoomTempDAO().selectRoomTempByRoomCode(getRoomCode());
			if(roomTempBO != null){
				ReserveClearRoomTempVO roomTempVO = new ReserveClearRoomTempVO();
				roomTempVO.setSeq(1);
				roomTempVO.setRoomId(roomTempBO.getRoomId());
				roomTempVO.setRoomCode(roomTempBO.getRoomCode());
				roomTempVO.setDateFrom(DateUtils.getDateStr(roomTempBO.getDateFrom(), false));
				roomTempVO.setDateTo(DateUtils.getDateStr(roomTempBO.getDateTo(), false));
				roomTempList.add(roomTempVO);
			}
		}
		return roomTempList;
	}
	
	public String processClearRoom(){
		ReserveClearRoomTempVO roomTemp = new ReserveClearRoomTempVO();
		GlobalUser globalUser = (GlobalUser)getSession().get(AppsindyConstants.S_USER_LOGIN);
		log.info("Clear room id = " + roomId + " :: userId = " + globalUser.getUserId());
		roomTemp.setUserLoginId(globalUser.getUserId());
		roomTemp.setOrgId(1);
		roomTemp.setRoomId(roomId);
		roomTemp.setStatusReserved(ReservConstants.ServiceStatusReserved);
		roomTemp.setStatusBooked(ReservConstants.ServiceStatusBooked);
		roomTemp.setStatusCheckin(ReservConstants.ServiceStatusCheckin);
		roomTemp.setStatusCheckout(ReservConstants.ServiceStatusCheckout);
		roomTemp.setStatusPayment(ReservConstants.ServiceStatusPayment);
		roomTemp.setStatusClear(ReservConstants.ServiceStatusClear);
		roomTemp.setAuthorizeChangbackStatus(ReservConstants.AuthorizeChangbackStatus);
		
		String json = null;
		try{
			getReserveClearRoomTempDAO().processClearRoom(roomTemp);
			json = "{'status':'Success'}";
		} catch(Exception e){
			json = "{'status':'Error'}";
		}
		setJsonModel(json);
        return JSON_RESULT;
	}
	
	public String getRoomTempList(){
		String json = "[{'roomCode':'101'},{'roomCode':'102'},{'roomCode':'201'}]";
		setJsonModel(json);
        return JSON_RESULT;
	}
	
	public ReserveClearRoomTempDAO getReserveClearRoomTempDAO(){
		return reserveClearRoomTempDAO;
	}
	public void setReserveClearRoomTempDAO(ReserveClearRoomTempDAO reserveClearRoomTempDAO){
		this.reserveClearRoomTempDAO = reserveClearRoomTempDAO;
	}

	public int getRoomId() {
		return roomId;
	}
	public void setRoomId(int roomId) {
		this.roomId = roomId;
	}

	public String getRoomCode() {
		return roomCode;
	}
	public void setRoomCode(String roomCode) {
		this.roomCode = roomCode;
	}
	
}
